import gtk,  subprocess,  os 
import feedparser,  webkit



class RSSThing():

    def __init__(self):
        #initialise stuff
        self.liststore=gtk.ListStore(str, str, str,str)
        self.window=gtk.Window()
        #make a list of feeds 
        #TODO Implement something to add or switch the current feed
        self.feedlist=["http://feeds.bbci.co.uk/news/rss.xml", "http://www.tuxradar.com/rss"]
        #populate our data by reading the first feed (see simple helper function)
        self.data=self.get_feed(0)
        self.fill_list_from_feed()
        
        #container widget for main window 
        hbox=gtk.HBox(False, 2)

        #webkit object        
        self.webview=webkit.WebView()
        
        # liststore.append([0,"the first"])
        self.treeview=gtk.TreeView(self.liststore)
        self.txtrenderer=gtk.CellRendererText()
        #self.column0 = gtk.TreeViewColumn('time', self.txtrenderer,text=0)
        self.column1 = gtk.TreeViewColumn('Feed summary', self.txtrenderer,text=1)
        #self.treeview.append_column(self.column0)
        self.treeview.append_column(self.column1)
        self.treeview.columns_autosize()
        scroll_window= gtk.ScrolledWindow()
        scroll_window.add(self.treeview)
        #self.treeview.set_size_request(-1, -1)
        scroll_window2=gtk.ScrolledWindow(None, None)
        scroll_window2.add(self.webview)
        
        self.treeview.connect("row-activated", self.tree_doubleclick)
        
        # create a list of menu items, with accelerator keys and methods 
        #TODO add menus for refreshing feed, adding new feeds etc.
        filemenuitems=[("Quit",  "<Control>Q", self.on_quit)]
        
        #create the menu bar (see LXF148)
        menubar = gtk.MenuBar()
        mainmenu = gtk.Menu()
        filemenu = gtk.MenuItem("_File")
        filemenu.set_submenu(mainmenu)
        accelgroup = gtk.AccelGroup()
        self.window.add_accel_group(accelgroup)
        #iterate through list to generate menu items
        for item in filemenuitems:
            newitem=gtk.MenuItem(item[0])
            #add shortcuts keys if they exist
            if (item[1] != ""):
                key, mod = gtk.accelerator_parse(item[1])
                newitem.add_accelerator("activate", accelgroup, key,mod, gtk.ACCEL_VISIBLE)
            newitem.connect("activate", item[2])
            mainmenu.append(newitem)
        menubar.append(filemenu)
        
        
        #create an HPaned for the webview and the listview
        panes=gtk.HPaned()
        panes.pack1(scroll_window,  True, True)
        panes.pack2(scroll_window2,  True,  True)
        
        #create a container for the main window and pack the menu and panes into it
        vbox = gtk.VBox(False, 2)
        vbox.pack_start(menubar, False, False, 0)
        vbox.pack_start(panes, True, True, 0)
        
        #pre-load a page into web view
        self.get_web("http://www.bbc.co.uk/news/")
        
        #set up window

        self.window.set_title("Really Simple RSS feed thing")
        self.window.set_size_request(800, 400)
        self.window.add(vbox)
        self.window.connect("destroy", gtk.main_quit)
        self.window.show_all()
        
    def on_quit(self, item):
        self.window.destroy()
        gtk.main_quit()
        
    def get_feed(self, index):
        return(feedparser.parse(self.feedlist[index]))
        
    def get_web(self, url):
        self.webview.open(url)
    
    def fill_list_from_feed(self):
        self.liststore.clear()
        ident=self.data.feed.title
        for i in range(len(self.data.entries)):
            #get the date/time
            time=self.data.entries[i].updated
            title=self.data.entries[i].title
            summary=self.data.entries[i].summary
            link=self.data.entries[i].link
            self.liststore.append([time, title, summary, link])
        return
        
    def tree_doubleclick(self, tree,  path,  column):
        # path conatins the index of the selection
        #we can use this to fetch the data directly from the liststore object
        url = self.liststore[path[0]][3]
        #NB the path is a tuple, so we need to index the first part of it to get the top-level row)
        self.get_web(url)
            

    

def main():
    gtk.main()
    

if __name__ == "__main__":
    RSSThing()
    main()
